package com.yuncheng.spcyApi.service.impl;

import cn.hutool.core.bean.BeanUtil;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuncheng.spcyApi.constant.common.LogConstant;
import com.yuncheng.spcyApi.constant.common.SpcyConstant;
import com.yuncheng.spcyApi.entity.SpcyRsGcy;

import com.yuncheng.spcyApi.mapper.SpcyRsGcyMapper;
import com.yuncheng.spcyApi.service.ISpcyLogService;
import com.yuncheng.spcyApi.service.ISpcyRsGcyService;
import com.yuncheng.spcyApi.utils.GetCurrentUserUtils;

import com.yuncheng.spcyApi.utils.GetUuIdUtils;
import com.yuncheng.vo.CurrentUserVo;
import org.springframework.stereotype.Service;

import com.yuncheng.common.base.service.impl.BaseServiceImpl;
import utils.LogUtils;
import utils.ObjectToMapUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description: 审评查验-观察员库
 * @Author: csx
 * @Date: 2023-03-03
 * @Version: V1.0
 */
@Service
public class SpcyRsGcyServiceImpl extends BaseServiceImpl<SpcyRsGcyMapper, SpcyRsGcy> implements ISpcyRsGcyService {

    @Resource
    private SpcyRsGcyMapper spcyRsGcyMapper;

    @Resource
    private ISpcyLogService spcyLogService;

    @Override
    public void impRsHcyData(Map map, String ywflCode, Integer xh) {
        SpcyRsGcy spcyRsGcy = new SpcyRsGcy();
        BeanUtil.copyProperties(map,spcyRsGcy); // 复制信息
        String rsGcyId = GetUuIdUtils.NotReplaceUuId();
        spcyRsGcy.setId(rsGcyId);
        spcyRsGcy.setFywflcode(ywflCode);

        String gcyXm = "";
        if (spcyRsGcy != null && StringUtils.isNotEmpty(spcyRsGcy.getFgcyxm())){
            gcyXm = spcyRsGcy.getFgcyxm().replaceAll("\\s*", "");
            gcyXm = gcyXm.replaceAll((char)12288+"","");
        }
        spcyRsGcy.setFgcyxm(gcyXm);
        spcyRsGcy.setForder(xh);

        CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();
        spcyRsGcy.setCreateTime(new Date());
        if (currentUser != null){
            spcyRsGcy.setCreateBy(currentUser.getName());
        }

        // 该业务分类下 观察员是否存在
        List<SpcyRsGcy> spcyRsGcies = spcyRsGcyMapper.selectList(new LambdaQueryWrapper<SpcyRsGcy>()
                .eq(SpcyRsGcy::getFgcyxm, gcyXm)
                .eq(SpcyRsGcy::getFywflcode, ywflCode));
        // 不存在新增
        if (CollectionUtils.isEmpty(spcyRsGcies)) {
            spcyRsGcyMapper.insert(spcyRsGcy); // 新增观察员

            String ywclNameByCode = SpcyConstant.getYwclNameByCode(ywflCode); // 业务分类名称
            if (spcyRsGcy != null) {
                String operContent = "新增导入观察员：";
                // 将map里面的属性和值转String
                Map<String, Object> beanMap = ObjectToMapUtils.objectToMap(spcyRsGcy);
                operContent += LogUtils.MapToStringMethod(beanMap);
                spcyLogService.addLogs(LogConstant.LOGS_RS_GCY, operContent, rsGcyId, LogConstant.LOGS_RS_GCY, SpcyConstant.LOGS_SYS_TYPE_XZ);
            }
        }else {
            // 不处理
        }
    }

    @Override
    public List<SpcyRsGcy> getRsGcyList(SpcyRsGcy spcyRsGcy) {
        LambdaQueryWrapper<SpcyRsGcy> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(spcyRsGcy.getFywflcode()),SpcyRsGcy::getFywflcode,spcyRsGcy.getFywflcode());
        queryWrapper.like(StringUtils.isNotBlank(spcyRsGcy.getFdwmc()),SpcyRsGcy::getFdwmc,spcyRsGcy.getFdwmc());
        queryWrapper.like(StringUtils.isNotBlank(spcyRsGcy.getFgcyxm()),SpcyRsGcy::getFgcyxm,spcyRsGcy.getFgcyxm());
        queryWrapper.like(StringUtils.isNotBlank(spcyRsGcy.getFszds()),SpcyRsGcy::getFszds,spcyRsGcy.getFszds());
        queryWrapper.orderByAsc(SpcyRsGcy::getForder);
        List<SpcyRsGcy> gcyList = list(queryWrapper);
        return gcyList;
    }
}
